﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections;

public partial class XSSJ_XSSJICList : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("5"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      if (CWSD.GetSDZT("是"))
      {
        Server.Transfer("~\\sd.aspx");
      }
      date1.Date = CommonUse.GetYC();
      date2.Date = CommonUse.GetYM();
    }
    BindGrid();
  }

  protected void BindGrid()
  {
    StringBuilder sbSql = new StringBuilder();
    sbSql.Append(@"select  ROW_NUMBER() OVER (ORDER BY a.cbsjid desc) AS xh,a.*,convert(varchar,a.CBSJ,23) BYCBSJ,
c.KHMC,C.IC,c.KH,c.XXDZ,c.LXDH,f.CBR,i.bqsj,i.jfsjid FROM 
    (select * from sjlr_cbsj where cbsj>=@0 and cbsj<=cast(@1 as datetime)) A 
    join sjlr_bqql b on a.cbsjid=b.cbsjid 
    left join jcxx_khxx c on a.khid=c.khid 
left join jcxx_cbr f on a.cbrid=f.cbrid
left join (select * from sjlr_jfsj i where 
JFSJ>=@0 and JFSJ<=cast(@1 as datetime)+1) i on a.khid=i.khid
WHERE a.dlm=@2 and ic='是' AND a.ljysql=i.jfsjid

");
    //sbSql.Append("order by a.cbsjid desc");
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 10),
                                  new SqlParameter("@2", SqlDbType.VarChar, 10)};
    cmdParams[0].Value = date1.Text;
    cmdParams[1].Value = date2.Text;
    cmdParams[2].Value = SysManage.Get_User;

    DataSet ds = DbHelperSQL.Query(sbSql.ToString(), cmdParams);
    ASPxGridView1.DataSource = ds;
    ASPxGridView1.DataBind();
  }

  protected void lbDelete_Command(object sender, CommandEventArgs e)
  {
    string sKHID = "", sRQ = "", bcyq = "";
    string[] s = e.CommandArgument.ToString().Split(new char[] { '_' });
    SqlDataReader dr = CBSJ.GetCBSJ(s[0]);
    if (dr.Read())
    {
      sKHID = dr["khid"].ToString();
      sRQ = dr["rq"].ToString();
      bcyq = dr["sjyql"].ToString();
    }
    dr.Close();

    if (CBSJ.ExistHQID(sKHID, sRQ))
    {
      ClientScriptManager cs = Page.ClientScript;
      cs.RegisterStartupScript(typeof(string), "TabJs", "<script language='javascript'>alert( '本次日期之后已有数据，不能修改！'); </script>");
      return;
    }
    List<CommandInfo> cis = new List<CommandInfo>();

    cis.Add(CommonUse.DeleteCI("SJLR_CBSJ", "cbsjid", s[0]));
    cis.Add(CommonUse.DeleteCI("SJLR_bqql", "cbsjid", s[0]));
    cis.Add(CommonUse.DeleteCI("sjlr_jfsj", "jfsjid", s[1]));
    cis.Add(CommonUse.DeleteCI("sjlr_jfmx", "jf_fksjid", s[1]));

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      ZHCX.CreateZHCX(sKHID, Convert.ToDateTime(sRQ).ToString("yyyy-MM-dd"));
      ZHCX.UpdateCB(sKHID, Convert.ToDateTime(sRQ).ToString("yyyy-MM-dd"), s[0]);
      ZHCX.UpdateJF(sKHID, Convert.ToDateTime(sRQ).ToString("yyyy-MM-dd"));
      BindGrid();
      string nr = "删除IC卡收费:客户编码为" + sKHID + " 日期为" + sRQ + " 本次用气为" + bcyq;
      rzcz.InsertRZ(nr);
    }
  }

  protected void btnQuery_Click(object sender, EventArgs e)
  {

  }

  protected void btnExport_Click(object sender, EventArgs e)
  {
    this.ASPxGridViewExporter1.WriteXlsToResponse();
  }
}